package com.bestcem.xm.txtanls.grpc.service.param.analysis;

import com.bestcem.xm.common.core.constant.RegTypes;
import com.bestcem.xm.txtanls.enums.ColumnDataTypeEnum;
import com.bestcem.xm.txtanls.service.dto.mq.ColumnChanged;
import com.bestcem.xm.txtanls.service.dto.mq.WorktableChanged;
import lombok.*;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Pattern;

/**
 * 数据源更新联动更新文本分析项目入参校验
 *
 * @author XiaoYunSong <ys.xiao@idiaoyan.com>
 * @version v1.0
 * @date 2021/7/15 17:12
 */
@NoArgsConstructor
@AllArgsConstructor
@Getter
@Setter
@ToString
public class LinkageUpdateParam {
    /**
     * id
     */
    @NotBlank(message = "id错误")
    @Pattern(regexp = RegTypes.OBJECT_ID, message = "id错误")
    private String id;

    /**
     * 名字
     */
    @NotBlank(message = "名字不能为空")
    private String name;

    /**
     * 是否为更新时间字段; 默认:否
     */
    private Boolean time;

    /**
     * 是否为更新数据源(当前值为 true 时,忽略 isTime 字段); 默认:否
     */
    private Boolean worktable;


    public static LinkageUpdateParam getInstance(WorktableChanged worktableChanged) {
        LinkageUpdateParam linkageUpdateParam = new LinkageUpdateParam();
        linkageUpdateParam.setId(worktableChanged.getId());
        linkageUpdateParam.setName(worktableChanged.getName());
        linkageUpdateParam.setTime(false);
        linkageUpdateParam.setWorktable(true);
        return linkageUpdateParam;
    }

    public static LinkageUpdateParam getInstance(ColumnChanged columnChanged) {
        LinkageUpdateParam linkageUpdateParam = new LinkageUpdateParam();
        linkageUpdateParam.setId(columnChanged.getId());
        linkageUpdateParam.setName(columnChanged.getReadableCol());
        linkageUpdateParam.setTime(ColumnDataTypeEnum.DATETIME.getIndex() == columnChanged.getDType());
        linkageUpdateParam.setWorktable(false);
        return linkageUpdateParam;
    }
}
